package com.powerunion.datacollection.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.powerunion.datacollection.service.SubmissionWorkItemManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class SubmissionWorkAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(SubmissionWorkAction.class);
	private  SubmissionWorkItemManager swManager = null;
	
	public SubmissionWorkAction() {
		super();
		this.swManager = new SubmissionWorkItemManager();
	}
	
	public void finalize() throws Throwable {
		this.swManager = null;
		super.finalize();
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userCode = request.getRemoteUser();
		String result = this.swManager.findSubmissionWorkByUser(userCode);
		logger.debug("SubmissionWork:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	public ActionForward updateState(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String swId = request.getParameter("swId");
		int state = Integer.parseInt(request.getParameter("state"));
		Transaction transaction = null;
		response.setContentType("text/json;charset=utf-8");
		try{
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			boolean flag = this.swManager.updateState(state, swId);
			
			if (flag == true) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
	
	public ActionForward updateState4User(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String swId = request.getParameter("swId");
		int state = Integer.parseInt(request.getParameter("state"));
		Transaction transaction = null;
		response.setContentType("text/json;charset=utf-8");
		try{
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			boolean flag = this.swManager.updateState4User(state, swId);
			
			if (flag == true) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
	
	public ActionForward submission(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String actId = request.getParameter("actId");
		String swId = request.getParameter("swId");
		String formId = request.getParameter("formId");
		String nextActivityId = request.getParameter("nextActivityId");
		//String user = request.getRemoteUser();
		Transaction transaction = null;
		response.setContentType("text/json;charset=utf-8");
		try{
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			String result = "";
			result = this.swManager.submission(actId,swId,formId,nextActivityId);
			
			if (StringUtils.isEmpty(result)) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true,data:'"+result+"'}");
			}
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
	
	public ActionForward completeWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userCode = request.getRemoteUser();
		String result = this.swManager.findCompleteSubmissionWorkByUser(userCode);
		logger.debug("complete submission work:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
//	public ActionForward getAllSubWorksByAct(ActionMapping mapping, ActionForm form,
//			HttpServletRequest request, HttpServletResponse response)
//			throws Exception {
//		String actId = request.getParameter("actId");
//		String procId = request.getParameter("procId");
//		List sws = this.swManager.getAllSubWorksByAct(procId,actId);
//		JSONArray jsonArr = JSONArray.fromObject(sws);
//		String result = "{results:"+jsonArr+"}";
//		logger.debug("getActAllSubWorkIdNames:"+result);
//		response.setContentType("text/json;charset=utf-8");
//		try{
//			response.getWriter().print(result);
//		}catch(Exception e) {
//			logger.error("输出json失败",e);
//			return null;
//		}
//		return null;
//	}
	
	public ActionForward allWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userCode = request.getRemoteUser();
		String result = this.swManager.findAllSubmissionWorkByUser(userCode);
		logger.debug("all submission work:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
}
